﻿using CodeCarvings.Piczard;
using CodeCarvings.Piczard.Filters.Watermarks;
using SAD_Assignment1.BLO;
using SAD_Assignment1.DTO;
using SAD_Assignment1.Shared;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SAD_Assignment1.Member
{
    public partial class UpPhoto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["account"] == null)
            {
                Response.Redirect(Resource.Login+"?redirect="+Resource.UpPhoto);
            }
            ImageUpload1.ImageUpload += ImageUpload1_ImageUpload;
            if (!this.IsPostBack)
            {
                this.ImageUpload1.AutoOpenImageEditPopupAfterUpload = true;
                //this.ImageUpload1.CropConstraint = new FixedCropConstraint(500, 200);
                //this.ImageUpload1.CropConstraint.DefaultImageSelectionStrategy = CropConstraintImageSelectionStrategy.WholeImage;
                //this.ImageUpload1.PreviewFilter = new FixedResizeConstraint(650, 3000);
                this.ImageUpload1.PostProcessingFilter = //new ScaledResizeConstraint(650, 1000) +
                     new ImageWatermark("~/Content/Photo/photovl.jpg", ContentAlignment.BottomCenter);
            }
        }

        protected void ImageUpload1_ImageUpload(object sender, SimpleImageUpload.ImageUploadEventArgs args)
        {
            if (ImageUpload1.SourceImageSize.Width < 300)
            {
                // The uploaded image is too small
                ImageUpload1.UnloadImage();
                ImageUpload1.SetCurrentStatusMessage("Image width must be at least 300 px");
                return;
            }
            
        }

        protected void upPhotoBtn_Command(object sender, CommandEventArgs e)
        {
            if (this.ImageUpload1.HasNewImage)
            {
                string pathName = "~/Content/Photo/" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".jpg";
                this.ImageUpload1.SaveProcessedImageToFileSystem(pathName, new JpegFormatEncoderParams(92));
                PhotoBLO photoBLO = new PhotoBLO();
                photoBLO.ResizePhoto(pathName);
                string email = ((AccountDTO)Session["account"]).Email;
                bool success = photoBLO.AddNewPhoto(txtTitle.Text, pathName, email);
                if (success)
                {
                    Response.Redirect(Resource.Vote);
                }
                else
                {
                    Response.Redirect(Resource.Info + "?msgID=2");
                }
            }
        }
    }
}